package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.List;
import java.util.TimeZone;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Logro;
import com.lostreseditores.sabersai.model.dto.LogroDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;


public class LogroView {
    private InputText txtLogrdescript;
    private InputText txtLogrletrav;
    private InputText txtLogrpuntajefinn;
    private InputText txtLogrpuntajeinin;
    private InputText txtRespidn;
    private InputText txtAsigidn_Asignatura;
    private InputText txtCursidn_Curso;
    private InputText txtTipoexamidn_Tipoexamen;
    private InputText txtLogridn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<LogroDTO> data;
    private LogroDTO selectedLogro;

    public LogroView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            LogroDTO logroDTO = (LogroDTO) e.getObject();

            if (txtLogrdescript == null) {
                txtLogrdescript = new InputText();
            }

            txtLogrdescript.setValue(logroDTO.getLogrdescript());

            if (txtLogrletrav == null) {
                txtLogrletrav = new InputText();
            }

            txtLogrletrav.setValue(logroDTO.getLogrletrav());

            if (txtLogrpuntajefinn == null) {
                txtLogrpuntajefinn = new InputText();
            }

            txtLogrpuntajefinn.setValue(logroDTO.getLogrpuntajefinn());

            if (txtLogrpuntajeinin == null) {
                txtLogrpuntajeinin = new InputText();
            }

            txtLogrpuntajeinin.setValue(logroDTO.getLogrpuntajeinin());

            if (txtRespidn == null) {
                txtRespidn = new InputText();
            }

            txtRespidn.setValue(logroDTO.getRespidn());

            if (txtAsigidn_Asignatura == null) {
                txtAsigidn_Asignatura = new InputText();
            }

            txtAsigidn_Asignatura.setValue(logroDTO.getAsigidn_Asignatura());

            if (txtCursidn_Curso == null) {
                txtCursidn_Curso = new InputText();
            }

            txtCursidn_Curso.setValue(logroDTO.getCursidn_Curso());

            if (txtTipoexamidn_Tipoexamen == null) {
                txtTipoexamidn_Tipoexamen = new InputText();
            }

            txtTipoexamidn_Tipoexamen.setValue(logroDTO.getTipoexamidn_Tipoexamen());

            if (txtLogridn == null) {
                txtLogridn = new InputText();
            }

            txtLogridn.setValue(logroDTO.getLogridn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtLogrdescript != null) {
            txtLogrdescript.setValue(null);
            txtLogrdescript.setDisabled(true);
        }

        if (txtLogrletrav != null) {
            txtLogrletrav.setValue(null);
            txtLogrletrav.setDisabled(true);
        }

        if (txtLogrpuntajefinn != null) {
            txtLogrpuntajefinn.setValue(null);
            txtLogrpuntajefinn.setDisabled(true);
        }

        if (txtLogrpuntajeinin != null) {
            txtLogrpuntajeinin.setValue(null);
            txtLogrpuntajeinin.setDisabled(true);
        }

        if (txtRespidn != null) {
            txtRespidn.setValue(null);
            txtRespidn.setDisabled(true);
        }

        if (txtAsigidn_Asignatura != null) {
            txtAsigidn_Asignatura.setValue(null);
            txtAsigidn_Asignatura.setDisabled(true);
        }

        if (txtCursidn_Curso != null) {
            txtCursidn_Curso.setValue(null);
            txtCursidn_Curso.setDisabled(true);
        }

        if (txtTipoexamidn_Tipoexamen != null) {
            txtTipoexamidn_Tipoexamen.setValue(null);
            txtTipoexamidn_Tipoexamen.setDisabled(true);
        }

        if (txtLogridn != null) {
            txtLogridn.setValue(null);
            txtLogridn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Logro entity = null;

        try {
            Integer logridn = new Integer(txtLogridn.getValue().toString());
            entity = BusinessDelegatorView.getLogro(logridn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtLogrdescript.setDisabled(false);
            txtLogrletrav.setDisabled(false);
            txtLogrpuntajefinn.setDisabled(false);
            txtLogrpuntajeinin.setDisabled(false);
            txtRespidn.setDisabled(false);
            txtAsigidn_Asignatura.setDisabled(false);
            txtCursidn_Curso.setDisabled(false);
            txtTipoexamidn_Tipoexamen.setDisabled(false);
            txtLogridn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtLogrdescript.setValue(entity.getLogrdescript());
            txtLogrdescript.setDisabled(false);
            txtLogrletrav.setValue(entity.getLogrletrav());
            txtLogrletrav.setDisabled(false);
            txtLogrpuntajefinn.setValue(entity.getLogrpuntajefinn());
            txtLogrpuntajefinn.setDisabled(false);
            txtLogrpuntajeinin.setValue(entity.getLogrpuntajeinin());
            txtLogrpuntajeinin.setDisabled(false);
            txtRespidn.setValue(entity.getRespidn());
            txtRespidn.setDisabled(false);
            txtAsigidn_Asignatura.setValue(entity.getAsignatura().getAsigidn());
            txtAsigidn_Asignatura.setDisabled(false);
            txtCursidn_Curso.setValue(entity.getCurso().getCursidn());
            txtCursidn_Curso.setDisabled(false);
            txtTipoexamidn_Tipoexamen.setValue(entity.getTipoexamen()
                                                     .getTipoexamidn());
            txtTipoexamidn_Tipoexamen.setDisabled(false);
            txtLogridn.setValue(entity.getLogridn());
            txtLogridn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveLogro(FacesUtils.checkString(
                    txtLogrdescript), FacesUtils.checkInteger(txtLogridn),
                FacesUtils.checkString(txtLogrletrav),
                FacesUtils.checkLong(txtLogrpuntajefinn),
                FacesUtils.checkLong(txtLogrpuntajeinin),
                FacesUtils.checkInteger(txtRespidn),
                FacesUtils.checkInteger(txtAsigidn_Asignatura),
                FacesUtils.checkInteger(txtCursidn_Curso),
                FacesUtils.checkInteger(txtTipoexamidn_Tipoexamen));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteLogro(FacesUtils.checkInteger(
                    txtLogridn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateLogro(FacesUtils.checkString(
                    txtLogrdescript), FacesUtils.checkInteger(txtLogridn),
                FacesUtils.checkString(txtLogrletrav),
                FacesUtils.checkLong(txtLogrpuntajefinn),
                FacesUtils.checkLong(txtLogrpuntajeinin),
                FacesUtils.checkInteger(txtRespidn),
                FacesUtils.checkInteger(txtAsigidn_Asignatura),
                FacesUtils.checkInteger(txtCursidn_Curso),
                FacesUtils.checkInteger(txtTipoexamidn_Tipoexamen));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtLogridn == null) {
                txtLogridn = new InputText();
            }

            txtLogridn.setValue(selectedLogro.getLogridn());

            BusinessDelegatorView.deleteLogro(FacesUtils.checkInteger(
                    txtLogridn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedLogro);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(String logrdescript, Integer logridn,
        String logrletrav, Long logrpuntajefinn, Long logrpuntajeinin,
        Integer respidn, Integer asigidn_Asignatura, Integer cursidn_Curso,
        Integer tipoexamidn_Tipoexamen) throws Exception {
        try {
            BusinessDelegatorView.updateLogro(logrdescript, logridn,
                logrletrav, logrpuntajefinn, logrpuntajeinin, respidn,
                asigidn_Asignatura, cursidn_Curso, tipoexamidn_Tipoexamen);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("LogroView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtLogrdescript() {
        return txtLogrdescript;
    }

    public void setTxtLogrdescript(InputText txtLogrdescript) {
        this.txtLogrdescript = txtLogrdescript;
    }

    public InputText getTxtLogrletrav() {
        return txtLogrletrav;
    }

    public void setTxtLogrletrav(InputText txtLogrletrav) {
        this.txtLogrletrav = txtLogrletrav;
    }

    public InputText getTxtLogrpuntajefinn() {
        return txtLogrpuntajefinn;
    }

    public void setTxtLogrpuntajefinn(InputText txtLogrpuntajefinn) {
        this.txtLogrpuntajefinn = txtLogrpuntajefinn;
    }

    public InputText getTxtLogrpuntajeinin() {
        return txtLogrpuntajeinin;
    }

    public void setTxtLogrpuntajeinin(InputText txtLogrpuntajeinin) {
        this.txtLogrpuntajeinin = txtLogrpuntajeinin;
    }

    public InputText getTxtRespidn() {
        return txtRespidn;
    }

    public void setTxtRespidn(InputText txtRespidn) {
        this.txtRespidn = txtRespidn;
    }

    public InputText getTxtAsigidn_Asignatura() {
        return txtAsigidn_Asignatura;
    }

    public void setTxtAsigidn_Asignatura(InputText txtAsigidn_Asignatura) {
        this.txtAsigidn_Asignatura = txtAsigidn_Asignatura;
    }

    public InputText getTxtCursidn_Curso() {
        return txtCursidn_Curso;
    }

    public void setTxtCursidn_Curso(InputText txtCursidn_Curso) {
        this.txtCursidn_Curso = txtCursidn_Curso;
    }

    public InputText getTxtTipoexamidn_Tipoexamen() {
        return txtTipoexamidn_Tipoexamen;
    }

    public void setTxtTipoexamidn_Tipoexamen(
        InputText txtTipoexamidn_Tipoexamen) {
        this.txtTipoexamidn_Tipoexamen = txtTipoexamidn_Tipoexamen;
    }

    public InputText getTxtLogridn() {
        return txtLogridn;
    }

    public void setTxtLogridn(InputText txtLogridn) {
        this.txtLogridn = txtLogridn;
    }

    public List<LogroDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataLogro();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<LogroDTO> logroDTO) {
        this.data = logroDTO;
    }

    public LogroDTO getSelectedLogro() {
        return selectedLogro;
    }

    public void setSelectedLogro(LogroDTO logro) {
        this.selectedLogro = logro;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
